package Util::Users::UserRegister;
use Image::Magick;
use Moose;
use namespace::autoclean;
use Image::Magick;
use File::Basename;

has 'schema' => (
	is => 'rw',
	required => 1,
	isa => 'DBIx::Class::Schema',
);

#####################################################
#Input: 
#      	image 
#####################################################
sub register{
	my ($self, $image, $username, $password, $fullname, $comm_item_id, $email, $static, $source, $source_nick, $university, $office, $house) = @_;

	my ($t_filename, $tmp_target, $small_image, $big_image, $f_suffix);

        if($image){
		my($filename, $directories, $suffix) = fileparse($image->filename, qr/\.[^.]*/);
		$f_suffix = $suffix;
		my $rand = new String::Random;
		$t_filename = $rand->randpattern("CCCCCcccccnnnnncccccCCCCC");

		my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
		$year = 1900 + $year;
		$mon = 1 + $mon;
		my $dir = "/$year/$mon/$mday";

		$t_filename = $t_filename . $suffix;
		$tmp_target = "/tmp/$dir/$t_filename";

		system("mkdir -p \"/tmp/$dir\"") unless(-e "/tmp/$dir");
		system("mkdir -p \"$static/small/$dir\"") unless(-e "$static/small/$dir");
		system("mkdir -p \"$static/big/$dir\"") unless(-e "$static/big/$dir");
		$small_image = "$static/small/$dir/$t_filename";
		$big_image = "$static/big/$dir/$t_filename";
		$t_filename = "$dir/$t_filename";

		unless ( $image->link_to($tmp_target) || $image->copy_to($tmp_target) ) {
			die( "Failed to copy '$filename' to '$tmp_target': $!" );
		}

		my $image_small = Image::Magick->new;
		$image_small->read($tmp_target);

		$image_small->Set( Gravity => 'Center' );
		$image_small->Resize( geometry => '54x54' );
		$image_small->Extent( geometry => '54x54' );
		$image_small->Write($small_image);

		my $image_big= Image::Magick->new;
		$image_big->read($tmp_target);
		$image_big->Set( Gravity => 'Center' );
		$image_big->Resize( geometry => '300x227' );
		$image_big->Extent( geometry => '300x227' );
		$image_big->Write($big_image);
	} else {
                $t_filename = "user/default.jpg";
        }

        my $user = $self->schema->resultset('User')->create({
                        username   => $username,
                        password  => $password,
                        photo   => $t_filename,
                        active => 'N',
                        full_name       => $fullname,
                        comm_item_id => $comm_item_id,
			university_id => $university,
			office_id => $office,
			house_id => $house,
                        email_address   => $email
		});
	unless($source eq '0') {
		$user->add_to_source_nick({
			source_id => $source,
			source_nick => $source_nick
		});
	}
	return $user;
}

sub change_photo{
	my ($self, $image, $static, $user) = @_;

	my ($t_filename, $tmp_target, $small_image, $big_image, $f_suffix);

	my($filename, $directories, $suffix) = fileparse($image->filename, qr/\.[^.]*/);
	$f_suffix = $suffix;
	my $rand = new String::Random;
	$t_filename = $rand->randpattern("CCCCCcccccnnnnncccccCCCCC");

	my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
	$year = 1900 + $year;
	$mon = 1 + $mon;
	my $dir = "/$year/$mon/$mday";

	$t_filename = $t_filename . $suffix;
	$tmp_target = "/tmp/$dir/$t_filename";

	system("mkdir -p \"/tmp/$dir\"") unless(-e "/tmp/$dir");
	system("mkdir -p \"$static/small/$dir\"") unless(-e "$static/small/$dir");
	system("mkdir -p \"$static/big/$dir\"") unless(-e "$static/big/$dir");
	$small_image = "$static/small/$dir/$t_filename";
	$big_image = "$static/big/$dir/$t_filename";
	$t_filename = "$dir/$t_filename";

	unless ( $image->link_to($tmp_target) || $image->copy_to($tmp_target) ) {
		die( "Failed to copy '$filename' to '$tmp_target': $!" );
	}

	my $image_small = Image::Magick->new;
	$image_small->read($tmp_target);

	$image_small->Set( Gravity => 'Center' );
	$image_small->Resize( geometry => '54x54' );
	$image_small->Extent( geometry => '54x54' );
	$image_small->Write($small_image);

	my $image_big= Image::Magick->new;
	$image_big->read($tmp_target);
	$image_big->Set( Gravity => 'Center' );
	$image_big->Resize( geometry => '260x197' );
	$image_big->Extent( geometry => '260x197' );
	$image_big->Write($big_image);

	unless($user->photo eq 'user/default.jpg') {
		my $pre_photo_small = $static. "/small/" . $user->photo; 
		my $pre_photo_big = $static. "/big/" . $user->photo; 
		system("rm $pre_photo_small");
		system("rm $pre_photo_big");
	}
	$user->update({photo => $t_filename});
	return $user;
}

__PACKAGE__->meta->make_immutable;

1;
